//
//  UIViewCommponent.m
//  HybirdNamibox
//
//  Created by 相颖 on 15/12/1.
//  Copyright © 2015年 Elephant. All rights reserved.
//

#import "UIViewCommponent.h"
#import <objc/runtime.h>
#import "KLUtils.h"
#import "UINavigationComponent.h"

static const void *hiddenNavgationBarKey = &hiddenNavgationBarKey;
static const void *animationingKey = &animationingKey;
static const void *NavgationPushCompletionBlockKey    = &NavgationPushCompletionBlockKey;

@implementation UIViewController (UIViewCommponent)
//  针对iOS16闪退修改
+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
//        replaceInstanceMethods([UIViewController class], NSSelectorFromString(@"commpontviewWillAppear:"), NSSelectorFromString(@"viewWillAppear:"));
        replaceInstanceMethods([UIViewController class], NSSelectorFromString(@"commpontviewDidAppear:"), NSSelectorFromString(@"viewDidAppear:"));
        replaceInstanceMethods([UIViewController class], NSSelectorFromString(@"commpontviewDidDisappear:"), NSSelectorFromString(@"viewDidDisappear:"));
    });
}

+(UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    return currentVC;
}

+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        rootVC = [rootVC presentedViewController];
    }
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        currentVC = rootVC;
    }
    
    return currentVC;
}


#pragma mark - navgationbar hidden

- (BOOL)hiddenNavgationBar{
    return [objc_getAssociatedObject(self, hiddenNavgationBarKey) boolValue];
}

- (void)setHiddenNavgationBar:(BOOL)hiddenNavgationBar{
    
    objc_setAssociatedObject(self, hiddenNavgationBarKey, [NSNumber numberWithBool:hiddenNavgationBar], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = !hiddenNavgationBar;
    }
}

- (void)setMetaInfo:(id)metaInfo{
    objc_setAssociatedObject(self, _cmd, metaInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)metaInfo{
    return objc_getAssociatedObject(self, @selector(setMetaInfo:));
}

- (BOOL)animationing{
    return [objc_getAssociatedObject(self, animationingKey) boolValue];
}

- (void)setAnimationing:(BOOL)animationing{
    objc_setAssociatedObject(self, animationingKey, [NSNumber numberWithBool:animationing], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)commpontviewWillAppear:(BOOL)animated{
     self.animationing = YES;
    if (self.parentViewController && self.parentViewController!=self.navigationController){
        [self commpontviewWillAppear:animated];
        return;
    }
    if (self.navigationController.navigationBarHidden!=self.hiddenNavgationBar) {
        [self.navigationController setNavigationBarHidden:self.hiddenNavgationBar animated:animated];
    }
    UIInterfaceOrientationMask orientation = [self supportedInterfaceOrientations];
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
        //如果当前的是竖屏模式，但是我支持的却是横屏模式，那么我需要强行转动
        if (orientation != UIInterfaceOrientationMaskPortrait && orientation!=UIInterfaceOrientationMaskAll &&orientation!=UIInterfaceOrientationMaskAllButUpsideDown) {
            [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeRight) forKey:@"orientation"];
        }
    }else{
        //当前是横屏模式，但是我只支持竖屏
        if (orientation == UIInterfaceOrientationMaskPortrait) {
            [[UIDevice currentDevice] setValue:@(UIApplication.sharedApplication.statusBarOrientation) forKey:@"orientation"];
            [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
        }
    }
    [self commpontviewWillAppear:animated];
}

- (NavgationPushCompletionBlock)completionBlock{
    return objc_getAssociatedObject(self,NavgationPushCompletionBlockKey);
}

- (void)setCompletionBlock:(NavgationPushCompletionBlock)completionBlock{
    objc_setAssociatedObject(self, NavgationPushCompletionBlockKey, completionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void)setDisableDarkModeDisplay:(BOOL)disableDarkModeDisplay{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
    if (@available(iOS 13.0, *)) {
        objc_setAssociatedObject(self, _cmd, [NSNumber numberWithBool:disableDarkModeDisplay], OBJC_ASSOCIATION_ASSIGN);
        if (disableDarkModeDisplay) {
            self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
        }else{
            self.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
        }
    }
#endif
}
- (BOOL)disableDarkModeDisplay{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
    return [objc_getAssociatedObject(self, @selector(setDisableDarkModeDisplay:)) boolValue];
#else
    return NO;
#endif
}

-(void)setDisableShowNativeAudio:(BOOL)disableShowNativeAudio{
    objc_setAssociatedObject(self, @selector(disableShowNativeAudio), [NSNumber numberWithBool:disableShowNativeAudio],OBJC_ASSOCIATION_ASSIGN);
}

-(BOOL)disableShowNativeAudio{
    return [objc_getAssociatedObject(self, @selector(disableShowNativeAudio)) boolValue];
}

- (void)commpontviewDidAppear:(BOOL)animated{
    if (self.completionBlock) {
        self.completionBlock();
        self.completionBlock = nil;
    }
    self.animationing = NO;
    [self commpontviewDidAppear:animated];
}

- (void)commpontviewDidDisappear:(BOOL)animated{
    if (self.completionBlock) {
        self.completionBlock();
        self.completionBlock = nil;
    }
    self.animationing = NO;
    [self commpontviewDidDisappear:animated];
}

#pragma mark - navpush
- (void)pushWithAnimator:(UIViewController *)vc complete:(void(^)(void))complete {
    [self.navigationController pushViewController:vc animated:YES completion:complete];
}

@end
